package ay.service.impl;

import ay.dao.MoodDao;
import ay.dao.UserDao;
import ay.dao.UserMoodPraiseRelDao;
import ay.dto.MoodDTO;
import ay.dto.UserDTO;
import ay.model.Mood;
import ay.model.User;
import ay.model.UserMoodPraiseRel;
import ay.mq.MoodConsumer;
import ay.mq.MoodProducer;
import ay.service.MoodService;
import ay.service.UserService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.jms.Destination;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service
public class MoodServiceImpl implements MoodService {

    @Autowired
    MoodDao moodDao;
    @Autowired
    UserDao userDao;
    @Autowired
    UserMoodPraiseRelDao userMoodPraiseRelDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserService userService;
    @Autowired
    private MoodProducer moodProducer;
    @Autowired
    private MoodConsumer moodConsumer;

    @Override
    public List<MoodDTO> findAll() {
        List<Mood> moodList = moodDao.findAll();
        return convertModel2DTO(moodList);
    }

    @Override
    public boolean praiseMood(String userId, String moodId) {
        UserMoodPraiseRel userMoodPraiseRel = new UserMoodPraiseRel();
        userMoodPraiseRel.setUserId(userId);
        userMoodPraiseRel.setMoodId(moodId);
        userMoodPraiseRelDao.save(userMoodPraiseRel);
        //更新点赞数目。
        Mood mood = this.findById(moodId);
        mood.setPraiseNum(mood.getPraiseNum()+1);
        this.update(mood);

        return Boolean.TRUE;
    }

    @Override
    public boolean update(Mood mood) {
        return moodDao.update(mood);
    }

    @Override
    public Mood findById(String id) {
        return moodDao.findById(id);
    }



//    redis

    private static final String PRAISE_HASH_KEY = "springMVC-learning.mood.id.list.key";

//    mq
    private static Destination destination = new ActiveMQQueue("ay.queue.high.concurrency.praise");

    @Override
    public boolean praiseMoodForRedis(String userId, String moodId) {
        MoodDTO moodDTO = new MoodDTO();
        moodDTO.setUserId(userId);
        moodDTO.setId(moodId);
        moodProducer.sendMessage(destination,moodDTO);
//        //1，存放到集合中
//        redisTemplate.opsForSet().add(PRAISE_HASH_KEY,moodId);
//        //2,存放到集合中
//        redisTemplate.opsForSet().add(moodId,userId);
        return false;
    }

    @Override
    public List<MoodDTO> findAllForRedis() {
        List<Mood> moodList = moodDao.findAll();
        if (CollectionUtils.isEmpty(moodList)){
            return Collections.EMPTY_LIST;
        }
        List<MoodDTO> moodDTOList = new ArrayList<MoodDTO>();
        for (Mood moodItem : moodList){
            MoodDTO moodDTO = new MoodDTO();
            moodDTO.setId(moodItem.getId());
            moodDTO.setUserId(moodItem.getUserId());
            //总点赞数量：数据库的点赞数量+redis点赞数量
            moodDTO.setPraiseNum(moodItem.getPraiseNum() + redisTemplate.opsForSet().size(moodItem.getId()).intValue());
            moodDTO.setPublishTime(moodItem.getPublishTime());
            moodDTO.setContent(moodItem.getContent());
            //通过userID查询用户
            User user = userService.find(moodItem.getUserId());
            moodDTO.setUserName(user.getName());
            moodDTO.setUserAccount(user.getAccount());
            moodDTOList.add(moodDTO);
        }
        return moodDTOList;
    }

    private List<MoodDTO> convertModel2DTO(List<Mood> moodList){
        if (CollectionUtils.isEmpty(moodList)){
            return Collections.EMPTY_LIST;
        }
        List<MoodDTO> moodDTOList = new ArrayList<MoodDTO>();
        for (Mood mood:moodList){
            MoodDTO moodDTO = new MoodDTO();
            moodDTO.setId(mood.getId());
            moodDTO.setContent(mood.getContent());
            moodDTO.setPraiseNum(mood.getPraiseNum());
            moodDTO.setUserId(mood.getUserId());
            moodDTO.setPublishTime(mood.getPublishTime());
            moodDTOList.add(moodDTO);
            //get user info; 1:*
            User user = userDao.find(moodDTO.getUserId());
            moodDTO.setUserName(user.getName());
            moodDTO.setUserAccount(user.getAccount());
        }
        return moodDTOList;
    }



}
